package com.fugui.onlinevideo.controller.admin;

import com.fugui.onlinevideo.dao.CourseVideoDao;
import com.fugui.onlinevideo.dao.ToolsItemDao;
import com.fugui.onlinevideo.entity.CourseVideo;
import com.fugui.onlinevideo.entity.ToolsItem;
import com.fugui.onlinevideo.entity.User;
import com.fugui.onlinevideo.service.CourseVideoService;
import com.fugui.onlinevideo.service.ToolsItemService;
import com.fugui.onlinevideo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
public class AdminController {

    @Autowired
    UserService userService;
    @Autowired
    CourseVideoService courseVideoService;
    @Autowired
    CourseVideoDao courseVideoDao;
    @Autowired
    ToolsItemDao toolsItemDao;
    @Autowired
    ToolsItemService toolsItemService;

    @RequestMapping("/admin")
    public String adminPage(Model model) {
        PageHelper.startPage(1, 8);
        PageInfo<User> userList = userService.getUserList();
        model.addAttribute("userList", userList);
        return "admin_main";
    }

    //用户列表，显示所有用户
    @RequestMapping(value = "/admin/user")
    public String user(Model model, Integer pageNum) {
        if (pageNum == null || pageNum <= 1) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 8);
        PageInfo<User> userList = userService.getUserList();
        model.addAttribute("userList", userList);
        return "admin_main";
    }
    // 显示更新用户页面
    @RequestMapping(value = "/admin/updatePage/{uid}")
    public String updatePage(@PathVariable Integer uid, Model model) {
        User user = userService.getUser(uid);
        model.addAttribute("user", user);
        return "admin_update_user";
    }
    // 处理用户更新请求
    @RequestMapping(value = "/admin/updateUser", method = RequestMethod.POST)
    public String updatePage(User user) {

        int code = userService.updateUser(user);

        return "redirect:/admin/user";
    }
    // 删除用户
    @RequestMapping(value = "/admin/userDel/{uid}")
    public String userDel(@PathVariable Integer uid, Model model, Integer pageNum) {

        int code = userService.delete(uid);
        if (pageNum == null || pageNum <= 1) {
            pageNum = 1;
        }
        return "redirect:/admin/user?pageNum=" + pageNum;
    }
    // 新增用户

    //视频列表，显示所有视频
    @RequestMapping(value = "/admin/video")
    public String video(Model model, Integer pageNum) {
        if (pageNum == null || pageNum <= 1) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 8);
        PageInfo<CourseVideo> videoList = courseVideoService.getVideoList();
        model.addAttribute("videoList", videoList);
        return "admin_video";
    }
    // 显示更新视频页面
    @RequestMapping(value = "/admin/updateVideo/{id}")
    public String updateVideo(@PathVariable Integer id, Model model) {

        List<Integer> list = new ArrayList<>();
        list.add(id);
        List<CourseVideo> ll = courseVideoDao.findCourseVideoByIds(list);
        model.addAttribute("video", ll.get(0));
        return "admin_update_video";
    }
    // 处理视频更新请求
    @RequestMapping(value = "/admin/updateVideo", method = RequestMethod.POST)
    public String updateVideo(CourseVideo video) {

        int code = courseVideoDao.updateCourseVideo(video);

        return "redirect:/admin/video";
    }
    // 删除视频
    @RequestMapping(value = "/admin/videoDel/{id}")
    public String videoDel(@PathVariable Integer id, Model model, Integer pageNum) {

        int code = courseVideoDao.deleteCourseVideoById(id);
        if (pageNum == null || pageNum <= 1) {
            pageNum = 1;
        }
        return "redirect:/admin/video?pageNum=" + pageNum;
    }
    // 新增视频

    //工具列表，显示所有工具
    @RequestMapping(value = "/admin/tool")
    public String tool(Model model, Integer pageNum) {
        if (pageNum == null || pageNum <= 1) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 8);
        PageInfo<ToolsItem> toolList = toolsItemService.getToolsItemAll();
        model.addAttribute("toolList", toolList);
        return "admin_tool";
    }
    // 显示更新工具页面
    @RequestMapping(value = "/admin/updateTool/{id}")
    public String updateTool(@PathVariable Integer id, Model model) {

        List<Integer> list = new ArrayList<>();
        list.add(id);
        List<ToolsItem> tools = toolsItemDao.findToolsItemByIds(list);
        model.addAttribute("tool", tools.get(0));

        return "admin_update_tool";
    }
    // 处理工具更新请求
    @RequestMapping(value = "/admin/updateTool", method = RequestMethod.POST)
    public String updateTool(ToolsItem tool) {

        int code = toolsItemDao.updateToolsItem(tool);

        return "redirect:/admin/tool";
    }
    // 删除工具
    @RequestMapping(value = "/admin/toolDel/{id}")
    public String toolDel(@PathVariable Integer id, Model model, Integer pageNum) {

        int code = toolsItemDao.deleteToolsItemById(id);
        if (pageNum == null || pageNum <= 1) {
            pageNum = 1;
        }
        return "redirect:/admin/tool?pageNum=" + pageNum;
    }
    // 新增工具


}
